package com.igeekhome.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.igeekhome.dto.EmpDto;
import com.igeekhome.mapper.SysStaffDtoMapper;
import com.igeekhome.mapper.SysStaffEncloseMapper;
import com.igeekhome.mapper.SysStaffMapper;
import com.igeekhome.pojo.DNetwork;
import com.igeekhome.pojo.SysStaff;
import com.igeekhome.pojo.SysStaffExample;
import com.igeekhome.service.IStaffService;
import com.igeekhome.util.CommonUtil;
import com.igeekhome.util.DtreeNode;
import com.igeekhome.util.ResultBean;
import com.igeekhome.util.TableResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author 汪璇
 */
@Service("sysStaffService")
public class SysStaffServiceImpl implements IStaffService {

    @Autowired
    private SysStaffMapper sysStaffMapper;
    
    @Autowired
    private SysStaffDtoMapper sysStaffDtoMapper;
    
    @Autowired
    private SysStaffEncloseMapper sysStaffEncloseMapper;

    @Override
    public List<SysStaff> selectAll() {
        List<SysStaff> sysStaffs = sysStaffMapper.selectByExample(null);
        return sysStaffs;
    }

    @Override
    public SysStaff login(Integer loginAccount, String password) {

        SysStaffExample example = new SysStaffExample();
        SysStaffExample.Criteria criteria = example.createCriteria();
            criteria.andLoginAccountEqualTo(loginAccount);

        List<SysStaff> sysStaffs = sysStaffMapper.selectByExample(example);

        //如果查询到的账号存在
        if (sysStaffs.size() != 0) {
            SysStaff staff = sysStaffs.get(0);
                if (CommonUtil.md5(password).equals(staff.getLoginPassword())) {
                    return staff;
                }

        }
        return null;
    }

    @Override
    public List<SysStaff> selectByIds(Integer rid, Integer nid) {
        HashMap<String,Integer> map = new HashMap<String,Integer>(2){
            {
                // 插入查询信息
                put("rid",rid);
                put("nid",nid);
            }
        };
        return sysStaffMapper.selectByRidAndNid(map);
    }
    
    
    /**
     *@author 汪璇
     * @Description 查配送员
    * @Param [network]
    * @Return java.util.List<com.igeekhome.pojo.SysStaff>
    **/
    @Override
    public ResultBean selectByNetworkId(DNetwork network,Integer type) {
        Integer networkId = network.getNetworkId();
        List<SysStaff> staffs = sysStaffMapper.selectByNetworkId(networkId, type);
        List<DtreeNode> nodes = new ArrayList<>();
        for (SysStaff staff : staffs) {
            DtreeNode node = new DtreeNode(staff.getStaffId().toString(),staff.getName());
            nodes.add(node);
        }
        return ResultBean.ok(nodes);
    }
    
    @Override
    public TableResultBean selectByNetworkId(DNetwork network, Integer page, Integer limit) {
        //开启分页
        PageHelper.startPage(page,limit);
        
        List<SysStaff> sysStaffs = sysStaffDtoMapper.selectByNetworkId(network.getNetworkId());
        long total = new PageInfo<SysStaff>(sysStaffs).getTotal();
        return TableResultBean.ok(total,sysStaffs);
    }
    
    /*
    * @author 汪璇
    *  @Description 查询员工详细信息进行员工管理
    * @Param [network, page, limit]
    * @Return com.igeekhome.util.TableResultBean
    **/
    @Override
    public TableResultBean selectDetailedByNetworkId(DNetwork network, Integer page, Integer limit) {
        //开启分页
        PageHelper.startPage(page,limit);
    
        List<EmpDto> sysStaffs = sysStaffDtoMapper.selectDetailedByNetworkId(network.getNetworkId());
        long total = new PageInfo<EmpDto>(sysStaffs).getTotal();
        return TableResultBean.ok(total,sysStaffs);
    }
    
    
    /*
    * @Description 增加员工并增加员工详细信息
    * @Param [network, empDto]
    * @Return java.lang.Integer
    **/
    @Transactional
    @Override
    public ResultBean backInsertId(DNetwork network,EmpDto empDto) {
        empDto.setAffiliatedNetworkId(network.getNetworkId());
        empDto.setStatus(1);
       
        // 设置回滚点
        Object savePoint = TransactionAspectSupport.currentTransactionStatus().createSavepoint();
        try {
            Integer id = sysStaffDtoMapper.backInsertId(empDto);
            System.out.println(empDto.getStaffId());
            empDto.setStaffId(empDto.getStaffId());
            sysStaffEncloseMapper.insertByEmpDto(empDto);
            return ResultBean.ok();
        } catch (Exception e) {
            e.printStackTrace();
            // 回滚到回滚点
            TransactionAspectSupport.currentTransactionStatus().rollbackToSavepoint(savePoint);
            return ResultBean.fail("数据提交失败,已回滚");
        }
    }
    
    
    /**
    * @Description 编辑员工信息
    * @Param [empDto]
    * @Return com.igeekhome.util.ResultBean
    **/
    @Transactional
    @Override
    public ResultBean editStaff(EmpDto empDto) {
        // 设置回滚点
        Object savePoint = TransactionAspectSupport.currentTransactionStatus().createSavepoint();
        try {
            sysStaffDtoMapper.UpdateByEmpDto(empDto);
            sysStaffEncloseMapper.updateByEmpDto(empDto);
            return ResultBean.ok();
        } catch (Exception e) {
            e.printStackTrace();
            // 回滚到回滚点
            TransactionAspectSupport.currentTransactionStatus().rollbackToSavepoint(savePoint);
            return ResultBean.fail("数据提交失败,已回滚");
        }
       
    }
    
    
    /**
    * @Description 删除员工信息
    * @Param [id] 根据id删除
    * @Return com.igeekhome.util.ResultBean
    **/
    @Transactional
    @Override
    public ResultBean delStaff(Integer id) {
        Object savePoint = TransactionAspectSupport.currentTransactionStatus().createSavepoint();
        try {
            sysStaffMapper.deleteByPrimaryKey(id);
            sysStaffEncloseMapper.deleteByPrimaryKey(id);
            return ResultBean.ok();
        } catch (Exception e) {
            e.printStackTrace();
            // 回滚到回滚点
            TransactionAspectSupport.currentTransactionStatus().rollbackToSavepoint(savePoint);
            return ResultBean.fail("数据提交失败,已回滚");
        }
    }

    @Override
    public ResultBean updateStaff(SysStaff sysStaff) {
        try {
            sysStaffMapper.update(sysStaff);
            return ResultBean.ok();
        } catch (Exception e) {
            return ResultBean.fail("操作失败");
        }
    }


}
